﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetOpenAuth.ApplicationBlock;
using DotNetOpenAuth.OAuth.Messages;
using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;
using DotNetOpenAuth.OpenId.RelyingParty;

using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OAuth.ChannelElements;
using DotNetOpenAuth.OpenId.Extensions.OAuth;
using OpenIdRelyingPartyWebForms;

using System.Diagnostics;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
using System.Configuration;

namespace YahooSamples.update
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(State.YahooAccessToken))
            {
                lresult.Text = "";
                if (Request.Params["s"] == "ok")
                {
                    lresult.Text = "Creating new update success";
                }
                else if (Request.Params["s"] == "err")
                {
                    lresult.Text = "Creating new update failed";
                }

            }
            else
            {
                Response.Redirect("~/login/Default.aspx");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.ttitle.Text) && !string.IsNullOrEmpty(this.ttitle.Text))
            {
                DateTime dt = DateTime.Now;
                DateTime unixRef = new DateTime(1970, 1, 1, 0, 0, 0);
                long pubdate = (dt.Ticks - unixRef.Ticks) / 10000000;

                string yql = "INSERT INTO social.updates (guid, title, description, link, pubDate, source, suid) VALUES (" +
                                "'" + State.UserGUID + "', '" + ttitle.Text + "', '" + tdescription.Text + "', '" + tlink.Text +
                                "', '" + pubdate + "', 'APP." + ConfigurationManager.AppSettings["yahooAppID"] + "', '" + 
                                Guid.NewGuid().ToString().Replace("-", "") + "')";
                string result = YahooConsumer.putYQL(Global.YahooWebConsumer, State.YahooAccessToken, yql);

                if (result.IndexOf("success") > 0)
                    Response.Redirect("~/update/Default.aspx?s=ok");
                else
                    Response.Redirect("~/update/Default.aspx?s=err");
            }
        }
    }
}